﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using Model;

namespace ACM
{
    public partial class Detail : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (BusinessID != 0)
                    GetTX(BusinessID);
                else
                    Status.ShowError("Incorrect parameter.");
            }
        }

        private void GetTX(int id)
        {
            hdfTxId.Value = id.ToString();

            GetACMTXCommand cmd = new GetACMTXCommand(id);
            cmd.Execute();

            if (cmd.Result.Count > 0)
            {
                acm_tx tx = cmd.Result[0];

                lblVIN.Text = tx.vin;
                lblSaleDate.Text = tx.adv_date != null ? tx.adv_date.Value.ToString("dd/MM/yyyy") : "-";
                lblDeliveryDate.Text = tx.dlv_date != null ? tx.dlv_date.Value.ToString("dd/MM/yyyy") : "-";
                lblCustomerName.Text = tx.customer_name;
                lblIdCard.Text = tx.id_card;
                lblAddressNo.Text = tx.addr_no;
                lblVillage.Text = tx.village;
                lblGroupNo.Text = tx.group_no;
                lblAlley.Text = tx.alley;
                lblBuilding.Text = tx.building_name;
                lblRoomNo.Text = tx.room_no;
                lblFloor.Text = tx.floor_no;
                lblRoad.Text = tx.road_name;
                lblParish.Text = tx.parish;
                lblDistrict.Text = tx.district;
                lblProvince.Text = tx.province;
                lblPostCode.Text = tx.postcode;
                decimal saleAmt = 0, deliAmt = 0;
                lblSaleCampaign.Text = GetCampaignName(tx.campaign_id_adv, ref saleAmt);
                lblDeliveryCampaign.Text = GetCampaignName(tx.campaign_id_dlv, ref deliAmt);
                //lblDiff.Text = (tx.diff_promotion_amount != null && tx.diff_promotion_amount.Value != 0) ? String.Format("{0:n2}", tx.diff_promotion_amount.Value) + " B" : "Samsung GALAXY Note 4";
                lblSeries.Text = "";
                lblSubseries.Text = "";
                acm_car_series s = GetCarSeries(tx.car_series_id);
                lblSeries.Text = s != null ? s.car_series : "-";
                lblSubseries.Text = s != null ? s.car_sub_series : "-";

                lblAttachFileName.Text = tx.attach1;
                lnkAttack.NavigateUrl = ConfigurationManager.AppSettings["UploadPath"] + tx.attach1;
                string Extension = Path.GetExtension(lnkAttack.NavigateUrl).ToLower();
                if (Extension == ".png" || Extension == ".jpg" || Extension == ".jpeg")
                {
                    lnkAttack.CssClass = "modalImage";
                    lnkAttack.Attributes.Add("data-effect", "mfp-zoom-in");
                }
                else
                {
                    lnkAttack.Attributes.Add("target", "_blank");
                }

                lblDiff.Text = String.Format("{0:n2}", deliAmt - saleAmt) + " B";

                if (tx.campaign_with_note4 == 1)
                {
                    lblSpecial.Text = (tx.mobile_note4 == 1) ? "รับ" : "ไม่รับ";
                }
                else
                {
                    lblSpecial.Text = "-";
                }
                
                lblCardType.Text = tx.passport != null ? (tx.passport.Value ? "Passport" : "บัตรประชาชน") : "บัตรประชาชน";

                CalculateSumCommand cmd2 = new CalculateSumCommand(tx.campaign_id_adv.Value, tx.campaign_id_dlv.Value, tx.mobile_note4.Value, tx.campaign_with_note4.Value);
                cmd2.Execute();
                lblSum.Text = String.Format("{0:n2}", Convert.ToDecimal(cmd2.Result)) + " B";
            }
        }

        private string GetCampaignName(int? campgn_id, ref decimal d)
        {
            d = 0;
            if (campgn_id == null) return "-";

            GetCampaignCommand cmd = new GetCampaignCommand(campgn_id.Value);
            cmd.Execute();
            if (cmd.Result.Count > 0)
            {
                d = cmd.Result[0].promotion_amount.Value;
                return cmd.Result[0].campaign_name + " - " + String.Format("{0:n2}", cmd.Result[0].promotion_amount) + " B";
            }

            return "-";
        }

        private acm_car_series GetCarSeries(int? id)
        {
            if (id == null) return null;

            GetCarSeriesCommand cmd = new GetCarSeriesCommand(id.Value);
            cmd.Execute();
            return cmd.Result;
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            Response.Redirect("Complete.aspx");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Response.Redirect("SaleKeyIn.aspx?id=" + BusinessID);
        }
    }
}